package io.renren.modules.secretary.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.enums.PunchStatus;
import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.ShiroUtils;
import io.renren.modules.secretary.service.StudentService;
import io.renren.modules.student.entity.PunchEntity;
import io.renren.modules.student.service.PunchService;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import static org.quartz.DateBuilder.newDate;


/**
 * @author guijing
 * @email sunlightcs@gmail.com
 * @date 2022-02-24 21:39:53
 */
@RestController
@RequestMapping("secretary/punch")
@Transactional
public class SecretaryPunchController {
    @Autowired
    private PunchService punchService;

    @Autowired
    private StudentService studentService;

    /**
     * 所有学生打卡记录列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) throws ParseException {
        SysUserEntity secretary = ShiroUtils.getUserEntity();
        java.sql.Date date = new java.sql.Date(System.currentTimeMillis());
        List<SysUserEntity> userEntities = studentService.AllStudentUserList();
        for (SysUserEntity student : userEntities) {
            QueryWrapper<PunchEntity> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id", student.getUserId());
            if (StringUtils.isBlank((String) params.get("punchDate"))) {
                wrapper.eq("punch_date", date);
            } else {
                wrapper.eq("punch_date", params.get("punchDate"));
            }
            PunchEntity punchEntity = punchService.getOne(wrapper);
            if (Objects.isNull(punchEntity)) {
                PunchEntity punch = new PunchEntity();
                punch.setPunchStatus(PunchStatus.NOPUNCH.getLevel());
                punch.setUserId(student.getUserId());
                if (StringUtils.isBlank((String) params.get("punchDate"))) {
                    punch.setPunchDate(date);
                } else {
                    punch.setPunchDate((java.sql.Date) params.get("punchDate"));
                }
                punchService.save(punch);
            }
        }
        if (StringUtils.isBlank((String) params.get("punchDate"))) {
            params.put("punchDate", date);
        }
        Page<Map<String, Object>> page = new Page<>( Long.valueOf(String.valueOf(params.get("page"))),  Long.valueOf(String.valueOf(params.get("limit"))));
        IPage<Map<String, Object>> mapIPage = punchService.getAllStudentList(page, params);
        PageUtils newPage = new PageUtils(mapIPage);

        return R.ok().put("page", newPage);
    }

    /**
     * 书记手动通过打卡
     *
     * @return
     */
    @RequestMapping("/passPunch/{punchId}")
    public R passPunch(@PathVariable("punchId") Long punchId) {
        PunchEntity punchEntity = new PunchEntity();
        punchEntity.setPunchId(punchId);
        punchEntity.setPunchTime(new java.util.Date());
        punchEntity.setPunchStatus(PunchStatus.ISPUNCH.getLevel());
        punchService.updateById(punchEntity);


        return R.ok();
    }

    /**
     * 书记手动驳回打卡
     *
     * @param punchId
     * @return
     */
    @RequestMapping("/refusePunch/{punchId}")
    public R refusePunch(@PathVariable("punchId") Long punchId) {
        PunchEntity punchEntity = new PunchEntity();
        punchEntity.setPunchId(punchId);
        punchEntity.setPunchStatus(PunchStatus.NOPUNCH.getLevel());
        punchService.updateById(punchEntity);
        return R.ok();
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{punchId}")
    public R info(@PathVariable("punchId") Long punchId) {
        PunchEntity punch = punchService.getById(punchId);

        return R.ok().put("punch", punch);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody PunchEntity punch) {
        punchService.save(punch);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody PunchEntity punch) {
        punchService.updateById(punch);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] punchIds) {
        punchService.removeByIds(Arrays.asList(punchIds));

        return R.ok();
    }

}
